package com.xjoyt.pay.share.services;

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.xjoyt.pay.abstracts.AbstractService;
import com.xjoyt.pay.api.ali.OssUtils;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.share.po.Bucket;
import com.xjoyt.pay.share.po.FileObejct;
import com.xjoyt.pay.share.repository.BucketRepository;
import com.xjoyt.pay.share.repository.FileObejctRepository;
import com.xjoyt.pay.share.vo.FileObjecResult;
import com.xjoyt.pay.share.vo.FileObjectParam;

@Service
public class FileObejctService extends AbstractService {

	@Autowired
	private FileObejctRepository repository;

	@Autowired
	private BucketRepository bucketRepository;

	@Autowired
	private RestTemplate restTemplate;

	public ExecuteResult<FileObejct> save(FileObejct entity) {
		if (entity.bucket == null) {
			Bucket bucket = bucketRepository.findOneByEnabledTrueAndDefaultBucketTrue();
			if (bucket == null)
				return ExecuteResult.fail("未设置默认储存");
			entity.bucket = bucket;
		}
		if (entity.addedDate == null) {
			entity.addedDate = new Date();
		}
		entity = repository.save(entity);
		return ExecuteResult.ok(entity);
	}

	public ExecuteResult<?> saveAllUrl(List<FileObejct> param) {
		Bucket bucket = bucketRepository.findOneByEnabledTrueAndDefaultBucketTrue();
		if (bucket == null)
			return ExecuteResult.fail("未设置默认储存");

		ExecutorService threadPool = Executors.newCachedThreadPool();
		FutureTask<Object> future = new FutureTask<>(() -> {
			for (FileObejct entity : param) {
				try {
					entity.bucket = bucket;
					saveUrl(entity);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return null;
		});
		threadPool.execute(future);

		return ExecuteResult.ok();
	}

	public ExecuteResult<FileObejct> saveUrl(FileObejct param) {
		if (param.bucket == null) {
			Bucket bucket = bucketRepository.findOneByEnabledTrueAndDefaultBucketTrue();
			if (bucket == null)
				return ExecuteResult.fail("未设置默认储存");
			param.bucket = bucket;
		}

		repository.findAllByKeyAndProperty(param.key, param.property).forEach(item -> {
			repository.delete(item);
			OssUtils.remove(item.bucket.endpoint, item.bucket.accessKey, item.bucket.secretKey, item.bucket.name,
					item.path);
		});

		ResponseEntity<byte[]> entity = restTemplate.getForEntity(param.path, byte[].class);
		if (!entity.hasBody() || entity.getBody() == null || entity.getBody().length < 1) {
			return ExecuteResult.fail("无效字节流");
		}

		String contentType = entity.getHeaders().getContentType().getType();
		OssUtils.upload(param.bucket.endpoint, param.bucket.accessKey, param.bucket.secretKey, param.bucket.name, null,
				contentType, new ByteArrayInputStream(entity.getBody()));
		return ExecuteResult.ok();
	}

	public List<FileObjecResult> findAllByKeysAndProperties(FileObjectParam param) {
		return repository.findAllByKeysAndProperties(param);
	}

}
